package com.js.service.bot.response;

import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.http.HttpRequest;
import com.js.common.annotation.ResponseClass;
import com.js.common.annotation.ResponseCommand;
import com.js.common.core.BotHandler;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import net.mamoe.mirai.utils.ExternalResource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

import java.awt.*;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Collections;

@Service
@Scope("prototype")
@ResponseClass("帮助")
public class HelpService extends BotHandler {

    @ResponseCommand(
            value = "#帮助",
            name = "获取帮助"
    )
    public MessageChain help(Long userId, Long targetId, String command, MessageChain messageChain) throws Exception {
        byte[] bytes = HttpRequest.post("http://127.0.0.1:7001/template").body("").execute().bodyBytes();
        MessageChainBuilder result = new MessageChainBuilder();
        ExternalResource resource = ExternalResource.create(new ByteArrayInputStream(bytes));
        result.append(getEvent().getSubject().uploadImage(resource.toAutoCloseable()));
        return result.build();
    }

    @ResponseCommand(
            value = "截图",
            name = "截图"
    )
    public MessageChain screenshot(Long userId, Long targetId, String command, MessageChain messageChain) throws Exception {
        MessageChainBuilder result = new MessageChainBuilder();
        ExternalResource resource = ExternalResource.create(new ByteArrayInputStream(screenshot()));
        result.append(getEvent().getSubject().uploadImage(resource.toAutoCloseable()));
        return result.build();
    }

    public byte[] screenshot() {
        System.getProperties().setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");
        ChromeOptions option = new ChromeOptions();
        //禁用沙盒
        option.addArguments("no-sandbox");
        //通过ChromeOptions的setExperimentalOption方法，传下面两个参数来禁止掉谷歌受自动化控制的信息栏
        option.setExperimentalOption("useAutomationExtension", false);
        option.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        //设置无头模式，后台运行，不弹出浏览器
        option.setHeadless(true);
        //设置浏览器大小
        option.addArguments("--window-size=1920,1080");
        //设置中文简体
        option.addArguments("--lang=zh-CN");
        //关闭GPU
        option.addArguments("--disable-gpu");
        ChromeDriver chromeDriver = new ChromeDriver(option);
        chromeDriver.manage().window().maximize();
        chromeDriver.get("file:///D:/svn/Yunzai-Bot/data/html/break/break.html");
//        byte[] bytes = ((TakesScreenshot)chromeDriver).getScreenshotAs(OutputType.BYTES);
        WebElement table1 = chromeDriver.findElement(By.id("table1"));
        ArrayList result = (ArrayList)((JavascriptExecutor) chromeDriver).executeScript(
                "return [$('#table1')[0].scrollHeight, $('#table1')[0].scrollWidth];", table1);
        System.out.println("table1: " + table1.getSize().getWidth() + ";" + table1.getSize().getHeight());
        System.out.println("table1: " + result.get(1) + ";" + result.get(0));
        Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting( 100 )).takeScreenshot(chromeDriver);
        Image image = screenshot.getImage();
        System.out.println("table1: " + image.getWidth(null) + ";" + image.getHeight(null));
//        image = ImgUtil.cut(Img.from(image).getImg(), new java.awt.Rectangle(0, 0, table1.getSize().getWidth(), image.getHeight(null)));
        return ImgUtil.toBytes(Img.from(image).getImg(), "PNG");
        /*Point p = element.getLocation();
        int width = element.getSize().getWidth();
        int height = element.getSize().getHeight();
        Image image = ImgUtil.cut(Img.from(screenshot.getImage()).getImg(), new java.awt.Rectangle(p.getX(), p.getY(), width, height));
        chromeDriver.quit();
        return ImgUtil.toBytes(image, "PNG");*/
    }
}
